﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using EntityObject;
using ThangNM_LIB.FUNC;

namespace DataAccessObject
{
    public class MediaDAO : DB_SQL_EXT
    {
        public Boolean Insert_Update_Delete(ref MediaEO _MediaEO, Int16 Insert_Update_Delete)
        {
            SqlCommand cmd = new SqlCommand("Media_Insert_Update_Delete");
            cmd.CommandType = CommandType.StoredProcedure;
            //Insert tblSEO
            setParamSEO(_MediaEO, ref cmd);

            cmd.Parameters.Add(new SqlParameter("@FK_iAccountID", NullEx.getNullValue(_MediaEO.FK_iAccountID)));
            cmd.Parameters.Add(new SqlParameter("@FK_sFileID", NullEx.getNullValue(_MediaEO.FK_sFileID)));
            cmd.Parameters.Add(new SqlParameter("@sLinkMedia", NullEx.getNullValue(_MediaEO.sLinkMedia)));
            cmd.Parameters.Add(new SqlParameter("@sContent", NullEx.getNullValue(_MediaEO.sContent)));
            cmd.Parameters.Add(new SqlParameter("@iTool", NullEx.getNullValue(_MediaEO.iTool)));
            cmd.Parameters.Add(new SqlParameter("@Insert_Update_Delete", Insert_Update_Delete));
            _MediaEO.PK_iSEO = ExecuteNonQuery_RetInt32(cmd);
            if (_MediaEO.PK_iSEO > 0)
                return true;
            else
                return false;
        }

        //public  Boolean Update(MediaEO _MediaEO)
        //{
        //    SqlCommand cmd = new SqlCommand("Media_Update", connMSSql);
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    ExecuteNonQuery(cmd);
        //    return true;
        //}

        //public  Boolean Update_iVisit_Or_iLike(MediaEO _MediaEO)
        //{
        //    SqlCommand cmd = new SqlCommand("Media_Update_iVisit_Or_iLike", connMSSql);
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new SqlParameter("@PK_iMediaID", _MediaEO.PK_iMediaID));
        //    //cmd.Parameters.Add(new SqlParameter("@iVisit", NullEx.getNullValue(_MediaEO.iVisit)));
        //    //cmd.Parameters.Add(new SqlParameter("@iLike", NullEx.getNullValue(_MediaEO.iLike)));
        //    ExecuteNonQuery(cmd);
        //    return true;
        //}

        //public  Boolean Delete(string _PK_iMediaID, String _loc_old)
        //{
        //    SqlCommand cmd = new SqlCommand("Media_Delete", connMSSql);
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new SqlParameter("@PK_iMediaID", _PK_iMediaID));
        //    cmd.Parameters.Add(new SqlParameter("@loc_old", _loc_old));
        //    ExecuteNonQuery(cmd);
        //    return true;
        //}

        public MediaEO SelectItem(Int32 _PK_iMediaID)
        {
            SqlCommand cmd = new SqlCommand("Media_SelectItem");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PK_iMediaID", _PK_iMediaID));
            return MediaDO(GetDataTable(cmd));
        }

        public DataTable SelectBy_FK_iCategoryID_iState(Int16 _FK_iCategoryID, Int16 _iState)
        {
            SqlCommand cmd = new SqlCommand("Media_SelectBy_FK_iCategoryID_iState");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@FK_iCategoryID", NullEx.getNullValue(_FK_iCategoryID)));
            cmd.Parameters.Add(new SqlParameter("@iState", NullEx.getNullValue(_iState)));
            return GetDataTable(cmd);
        }

        //public  MediaEO SelectBy_iLike_FK_iCategoryID(Int32 _iLike, Int16 _FK_iCategoryID)
        //{
        //    SqlCommand cmd = new SqlCommand("Media_SelectBy_iLike_FK_iCategoryID", connMSSql);
        //    cmd.CommandType = CommandType.StoredProcedure;
        //    cmd.Parameters.Add(new SqlParameter("@iLike", _iLike));
        //    cmd.Parameters.Add(new SqlParameter("@FK_iCategoryID", _FK_iCategoryID));
        //    return MediaDO(GetDataTable(cmd));
        //}

        public DataSet SelectBy_PK_iMediaID(Int32 _PK_iMediaID)
        {
            SqlCommand cmd = new SqlCommand("Media_SelectBy_PK_iMediaID");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@PK_iMediaID", _PK_iMediaID));
            return GetDataSet(cmd);
        }

        public DataTable SelectBy_FK_iAccountID(Int32 _FK_iAccountID)
        {
            SqlCommand cmd = new SqlCommand("Media_SelectBy_FK_iAccountID");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@FK_iAccountID", _FK_iAccountID));
            return GetDataTable(cmd);
        }

        public MediaEO SelectBy_iTool(Int16 _iTool)
        {
            SqlCommand cmd = new SqlCommand("Media_SelectBy_iTool");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@iTool", _iTool));
            return MediaDO(GetDataTable(cmd));
        }

        public DataTable SelectBy_All_Tool()
        {
            SqlCommand cmd = new SqlCommand("Media_SelectBy_iTool");
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@iTool", Convert.ToInt16(0)));
            return GetDataTable(cmd);
        }

        public MediaEO MediaDO(DataTable input)
        {
            List<MediaEO> lst = new List<MediaEO>();
            lst = (List<MediaEO>)ConvertEx.ConvertDataTableToListObject<MediaEO>(input, lst.GetType());
            if (CommonsCheck.ListObject_OK(lst))
                return lst[0];
            else
                return null;
        }
    }
}